package com.ruoyi.web.controller.store;

import java.util.HashMap;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.CommonResult;
import com.ruoyi.store.bo.HmSystemFormCheckBo;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.store.vo.HmSystemConfigVo;
import com.ruoyi.store.bo.HmSystemConfigQueryBo;
import com.ruoyi.store.bo.HmSystemConfigAddBo;
import com.ruoyi.store.bo.HmSystemConfigEditBo;
import com.ruoyi.store.service.IHmSystemConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 商城配置Controller
 *
 * @author ruoyi
 * @date 2021-07-04
 */
@Api(value = "商城配置控制器", tags = {"商城配置管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/store/config")
public class HmSystemConfigController extends BaseController {

	private final IHmSystemConfigService iHmSystemConfigService;

	/**
	 * 查询商城配置列表
	 */
	@ApiOperation("查询商城配置列表")
	@PreAuthorize("@ss.hasPermi('store:config:list')")
	@GetMapping("/list")
	public TableDataInfo<HmSystemConfigVo> list(@Validated HmSystemConfigQueryBo bo) {
		return iHmSystemConfigService.queryPageList(bo);
	}

	/**
	 * 导出商城配置列表
	 */
	@ApiOperation("导出商城配置列表")
	@PreAuthorize("@ss.hasPermi('store:config:export')")
	@Log(title = "商城配置", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult<HmSystemConfigVo> export(@Validated HmSystemConfigQueryBo bo) {
		List<HmSystemConfigVo> list = iHmSystemConfigService.queryList(bo);
		ExcelUtil<HmSystemConfigVo> util = new ExcelUtil<HmSystemConfigVo>(HmSystemConfigVo.class);
		return util.exportExcel(list, "商城配置");
	}

	/**
	 * 获取商城配置详细信息
	 */
	@ApiOperation("获取商城配置详细信息")
	@PreAuthorize("@ss.hasPermi('store:config:query')")
	@GetMapping("/{id}")
	public AjaxResult<HmSystemConfigVo> getInfo(@NotNull(message = "主键不能为空")
												@PathVariable("id") Integer id) {
		return AjaxResult.success(iHmSystemConfigService.queryById(id));
	}

	/**
	 * 查询配置表信息
	 * @param formId Integer
	 */
	@ApiOperation(value = "详情")
	@RequestMapping(value = "/info", method = RequestMethod.GET)
	public CommonResult<HashMap<String, String>> info(@RequestParam(value = "formId") Integer formId){
		return CommonResult.success(iHmSystemConfigService.info(formId));
	}

	/**
	 * 新增商城配置
	 */
	@ApiOperation("新增商城配置")
	@PreAuthorize("@ss.hasPermi('store:config:add')")
	@Log(title = "商城配置", businessType = BusinessType.INSERT)
	@RepeatSubmit
	@PostMapping()
	public AjaxResult<Void> add(@Validated @RequestBody HmSystemConfigAddBo bo) {
		return toAjax(iHmSystemConfigService.insertByAddBo(bo) ? 1 : 0);
	}

	/**
	 * 修改商城配置
	 */
	@ApiOperation("修改商城配置")
	@PreAuthorize("@ss.hasPermi('store:config:edit')")
	@Log(title = "商城配置", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping()
	public AjaxResult<Void> edit(@Validated @RequestBody HmSystemConfigEditBo bo) {
		return toAjax(iHmSystemConfigService.updateByEditBo(bo) ? 1 : 0);
	}
	/**
	 * 整体保存表单数据
	 * @param systemFormCheckRequest SystemFormCheckRequest 新增参数
	 * @author Mr.Zhang
	 * @since 2020-04-13
	 */
	@ApiOperation(value = "整体保存表单数据")
	@RequestMapping(value = "/save/form", method = RequestMethod.POST)
	public CommonResult<String> saveFrom(@RequestBody @Validated HmSystemFormCheckBo systemFormCheckRequest){
		if(iHmSystemConfigService.saveForm(systemFormCheckRequest)){
			return CommonResult.success();
		}else{
			return CommonResult.failed();
		}
	}
	/**
	 * 删除商城配置
	 */
	@ApiOperation("删除商城配置")
	@PreAuthorize("@ss.hasPermi('store:config:remove')")
	@Log(title = "商城配置" , businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
								   @PathVariable Integer[] ids) {
		return toAjax(iHmSystemConfigService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
	}

	/**
	 * 根据key获取表单配置数据
	 * @param key 配置表的的字段
	 * @return
	 */
	@ApiOperation(value = "表单配置根据key获取")
	@RequestMapping(value = "/getuniq", method = RequestMethod.GET)
	public CommonResult<Object> justGetUniq(@RequestParam String key){
		return CommonResult.success(iHmSystemConfigService.getValueByKey(key),"success");
	}
}
